<?php
use Doctrine\ORM\EntityManager,
    Doctrine\ORM\Configuration,
	Doctrine\ORM\Tools\Setup;
 
define('DEBUGGING', FALSE);
 
class Doctrine {
 
    public $em = null;
 	private static $o_instance;
    
    public function __construct()
    {
  	  if (!class_exists("Doctrine\Common\Version", false)) {
			
		// See :doc:`Configuration <../reference/configuration>` for up to date autoloading details.
		
		FileLoader::getInstance()->loadFile(C_FW_fwPackage . 'libraries/Doctrine/Common/ClassLoader.php');
		
		//FileLoader::getInstance()->loadFile(C_FW_fwPackage . 'libraries/Doctrine/Common/ClassLoader.php');
		$doctrineExtenstionsClassLoader = new \Doctrine\Common\ClassLoader('DoctrineExtensions', C_FW_fwPackage . 'libraries/Doctrine');
		$doctrineExtenstionsClassLoader->register();
		$doctrineClassLoader = new \Doctrine\Common\ClassLoader('Doctrine', C_FW_fwPackage . 'libraries');
		$doctrineClassLoader->register();
		
		$entitiesClassLoader = new \Doctrine\Common\ClassLoader('models', C_FW_customPackage);
		$entitiesClassLoader->register();
		
		$proxiesClassLoader = new \Doctrine\Common\ClassLoader('Proxies', C_FW_customPackage . 'models');
		$proxiesClassLoader->register();
		
		$symfonyClassLoader = new \Doctrine\Common\ClassLoader('Symfony', C_FW_fwPackage . 'libraries/Doctrine');
		$symfonyClassLoader->register();
		}
    	$env = 'development';
    	if ($env == 'development') {
    		$cache = new \Doctrine\Common\Cache\ArrayCache;
    	} else {
    		$cache = new \Doctrine\Common\Cache\ApcCache;
    	}
    	
	    // Set some configuration options
	    $config = new Configuration;
	    
	    // Metadata driver
	    $driverImpl = $config->newDefaultAnnotationDriver(C_FW_customPackage . 'models');
	    $config->setMetadataDriverImpl($driverImpl);
	    
	    // Caching
	    $config->setMetadataCacheImpl($cache);
	    $config->setQueryCacheImpl($cache);
	    
	    // Proxies
	    $config->setProxyDir(C_FW_customPackage . 'models/Proxies');
	    $config->setProxyNamespace('Proxies');
	    
	    if ($env == 'development') {
	    	$config->setAutoGenerateProxyClasses(TRUE);
	    } else {
	    	$config->setAutoGenerateProxyClasses(FALSE);
	    }
	    
	    // SQL query logger
	    if (DEBUGGING)
	    {
	    	$logger = new \Doctrine\DBAL\Logging\EchoSQLLogger;
	    	$config->setSQLLogger($logger);
	    }
    
	    
    	$config->setProxyDir( C_FW_customPackage . 'models/proxies');
    	$config->setProxyNamespace('test_claycode\Proxies');
    	// database configuration parameters
    	$conn = array(
    	    'driver' => 'pdo_mysql',
    	    'user' =>'root',
    	    'password' => '',
    	    'host' => 'localhost',
    	    'dbname' => 'test_claycode'
    	);
    	//include the dbConfig file from the custom
    	include C_FW_customPackage . 'configs/db_config.php';
    	//custom db configuration is there
    	if(isset($arr_dbConfig)){
    		$conn = $arr_dbConfig;
    	}
    	
    	// obtaining the entity manager
    	$this->em = \Doctrine\ORM\EntityManager::create($conn, $config);
    }
    
    public static function get()
    {
    	if(!isset(self::$o_instance) ||  self::$o_instance == null) {
    		self::$o_instance = new Doctrine();
    	}
    	return self::$o_instance;
    }
}